<?php
/**
 *  * author 韩天尊
 * Date: 2022/3/24
 * Email: <724700483@qq.com>
 */

namespace App\Toolys\LinkGatewayworker;
use GatewayClient\Gateway;

/**
 * 封装一个基类实现结合gatewayworker自动调用
 */
class Specification extends NewActions
{

    public string $_onMessageFromClientId;
    const REGISTERADDRESS = "172.18.0.3:1238";

    /** 原始已支持方法
     * @var array|string[]
     */
    static array $actions = [
        "sendToAll","sendToClient","closeClient","isOnline","bindUid","unbindUid","isUidOnline","getClientIdByUid","getUidByClientId","sendToUid","joinGroup",
        "leaveGroup","ungroup","sendToGroup","getClientIdCountByGroup","getClientSessionsByGroup","getAllClientIdCount","getAllClientSessions","setSession",
        "updateSession","getSession","getClientIdListByGroup","getAllClientIdList","getUidListByGroup","getUidCountByGroup","getAllUidList","getAllUidCount",
        "getAllGroupIdList",
    ];

    /** 自定义新封装方法
     * @var array
     */
    static array $newActions = [];

    /**
     * @param string $action    调用的方法
     * @param array $data       调用传入数据
     */
    public function __construct(string $action, array $data, string $_onMessageFromClientId='')
    {
        Gateway::$registerAddress = self::REGISTERADDRESS;
        $this->_onMessageFromClientId = $_onMessageFromClientId;
        $this->invoke($action, $data);//调用
    }

    /** 魔术方法 调用
     * @author 韩天尊
     * @date 2022-03-24
     */
    function invoke(/*$method, $param1, $param2, ...*/){
        $params = func_get_args();//数据
        $method = array_shift($params);//具体方法

        $params = $this->checkData($method, $params[0]);

        if (in_array($method, self::$actions)){
            return Gateway::$method(...$params);
        }
        if (in_array($method, self::$newActions)){
            return $this->{$method}(...$params);
        }
        return false;
    }

    /** 检测传入数据  难点在于$data  需要模板规则化高一致性
     * @param string $action
     * @param array $data
     * @return array
     * @author 韩天尊
     * @date 2022-03-24
     */
    function checkData(string $action, array $data): array
    {
        $data = arrayTransitionObject($data);
        switch ($action){
            case "sendToUid":{
                //列如信息写入数据库之类的...
                break;
            }
        }
        return objectTransitionArray($data);
    }
}